<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/17 9:26
 * Modified Time: 2019/7/17 9:26
 * Version: 2.0
 */

namespace App\Modules\Store\Criterias;


use App\Lib\Facades\FLAuth;
use App\Lib\Utilities;
use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;
use Illuminate\Support\Facades\DB;

class GetStoreInfoAppCriteria extends BaseCriterias
{
    protected $store_id;

    public function __construct($store_id)
    {
        $this->store_id = $store_id;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $store_id = $this->store_id;

        $store_resource_query = DB::table('store_resource')->selectRaw('  count(*) as store_resource_num,type,store_id')->where(['store_id' => $store_id,'type'=>1])
            ->groupBy('type');

        $select = [
            'hx_store.id as store_id',
            'hx_store.name as store_name',
            'hx_logo.image as store_logo',
            'DATEDIFF(now(),hx_store.created_at) + 1 as open_days',
            'hx_store.address as store_address',
            'hx_store.doorplate as store_doorplate',
            'hx_store.phone as store_phone',
            'hx_store.wechat as store_wechat',
            'hx_store.open_time',
            'hx_store.close_time',
            'hx_store.adcode',
            'hx_store.order_sum',
            'hx_store.longitude',
            'hx_store.latitude',
            'hx_store.water_expenditure',
            'hx_store.room_expenditure',
            'hx_store.internal_expenditure',
            'hx_province.name as province',
            'hx_city.name as city',
            'hx_district.name as district',
            'hx_store.address',
            'expenditure',
            'IF(hx_store_resource.store_resource_num=3,1,0) as is_intr_perfect',
            'IF(hx_store.alipay IS NOT NULL && hx_store.wxpay IS NOT NULL,1,0) as is_payment_code',
            'alipay',
            'wxpay'
        ];

        $select = implode($select, ',');

        $model = $model->selectRaw($select)
            ->leftJoin('picture as logo', 'store.logo', 'logo.id')
            ->withCount(['photo'])
            ->with(['alipay'=>function($query){
                $query->select(['id','image']);
            }])
            ->with(['wxpay'=>function($query){
                $query->select(['id','image']);
            }])
            ->leftJoin(\DB::raw("({$store_resource_query->toSql()}) as hx_store_resource"), function ($join) use ($store_resource_query) {
                $join->mergeBindings($store_resource_query)->on('store.id', '=', 'store_resource.store_id');
            })
            ->leftJoin('city as district','store.adcode','code')
            ->leftJoin('city','district.p_code','city.code')
            ->leftJoin('city as province','city.p_code','province.code')
            ->where('store.id', $store_id);

        return $model;
    }
}